class Term::ANSIColor::Attribute

Attributes

background[W]
name[R]
rgb[R]

Public Class Methods

[](name, true_coloring: false) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 33
def self.[](name, true_coloring: false)
  true_coloring ||= Term::ANSIColor.true_coloring?
  if true_coloring
    case
    when self === name                              then name
    when Array === name                             then true_color name
    when name.respond_to?(:to_rgb_triple)           then true_color(name.to_rgb_triple.to_a)
    when name.to_s =~ /\A(on_)?(\d+)\z/             then get "#$1color#$2"
    when name.to_s =~ /\A#([0-9a-f]{3}){1,2}\z/i    then true_color name
    when name.to_s =~ /\Aon_#([0-9a-f]{3}){1,2}\z/i then on_true_color name
    else                                            get name
    end
  else
    case
    when self === name                              then name
    when Array === name                             then nearest_rgb_color name
    when name.respond_to?(:to_rgb_triple)           then nearest_rgb_color(name.to_rgb_triple.to_a)
    when name.to_s =~ /\A(on_)?(\d+)\z/             then get "#$1color#$2"
    when name.to_s =~ /\A#([0-9a-f]{3}){1,2}\z/i    then nearest_rgb_color name
    when name.to_s =~ /\Aon_#([0-9a-f]{3}){1,2}\z/i then nearest_rgb_on_color name
    else                                            get name
    end
  end
end
attributes(&block) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 17
def self.attributes(&block)
  @__order__.map { |name| @__store__[name] }
end
get(name) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 58
def self.get(name)
  @__store__[name.to_sym]
end
named_attributes(&block) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 70
def self.named_attributes(&block)
  @named_attributes ||= attributes.reject(&:rgb_color?).each(&block)
end
nearest_rgb_color(color, options = {}) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 74
def self.nearest_rgb_color(color, options = {})
  rgb = RGBTriple[color]
  colors = rgb_colors(options)
  colors.reject(&:background?).min_by { |c| c.distance_to(rgb, options) }
end
nearest_rgb_on_color(color, options = {}) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 80
def self.nearest_rgb_on_color(color, options = {})
  rgb = RGBTriple[color]
  colors = rgb_colors(options)
  colors.select(&:background?).min_by { |c| c.distance_to(rgb, options) }
end
new(name, code, options = {}) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 96
def initialize(name, code, options = {})
  @name       = name.to_sym
  @background = @name.start_with?('on_')
  @code       = code.to_s
  if rgb = options[:true_color]
    @true_color = true
    @rgb = rgb
  elsif html = options[:html]
    @rgb = RGBTriple.from_html(html)
  elsif !options.empty?
    @rgb = RGBTriple.from_hash(options)
  else
    @rgb = nil # prevent instance variable not initialized warnings
  end
end
on_true_color(color, options = {}) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 91
def self.on_true_color(color, options = {})
  rgb = RGBTriple[color]
  new(:on_true, "", { true_color: rgb, background: true })
end
rgb_colors(options = {}, &block) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 62
def self.rgb_colors(options = {}, &block)
  colors = @rgb_colors ||= attributes.select(&:rgb_color?)
  if options.key?(:gray) && !options[:gray]
    colors = colors.reject(&:gray?)
  end
  colors.each(&block)
end
set(name, code, options = {}) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 9
def self.set(name, code, options = {})
  name = name.to_sym
  result = @__store__[name] = new(name, code, options)
  @__order__ << name
  @rgb_colors = nil
  result
end
true_color(color, options = {}) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 86
def self.true_color(color, options = {})
  rgb = RGBTriple[color]
  new(:true, "", { true_color: rgb, background: false })
end

Public Instance Methods

apply(string = nil, &block) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 124
def apply(string = nil, &block)
  ::Term::ANSIColor.color(self, string, &block)
end
background?() click to toggle source
# File lib/term/ansicolor/attribute.rb, line 128
def background?
  !!@background
end
code() click to toggle source
# File lib/term/ansicolor/attribute.rb, line 114
def code
  if true_color?
    background? ? "48;2;#{@rgb.to_a * ?;}" : "38;2;#{@rgb.to_a * ?;}"
  elsif rgb_color?
    background? ? "48;5;#{@code}" : "38;5;#{@code}"
  else
    @code
  end
end
distance_to(other, options = {}) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 152
def distance_to(other, options = {})
  if our_rgb = to_rgb_triple and
    other.respond_to?(:to_rgb_triple) and
    other_rgb = other.to_rgb_triple
  then
    our_rgb.distance_to(other_rgb, options)
  else
    1 / 0.0
  end
end
gradient_to(other, options = {}) click to toggle source
# File lib/term/ansicolor/attribute.rb, line 163
def gradient_to(other, options = {})
  if our_rgb = to_rgb_triple and
      other.respond_to?(:to_rgb_triple) and
      other_rgb = other.to_rgb_triple
    then
    true_coloring = options[:true_coloring] || Term::ANSIColor.true_coloring?
    our_rgb.gradient_to(other_rgb, options).map do |rgb_triple|
      if true_coloring
        self.class.true_color(rgb_triple, options)
      else
        self.class.nearest_rgb_color(rgb_triple, options)
      end
    end
  else
    []
  end
end
gray?() click to toggle source
# File lib/term/ansicolor/attribute.rb, line 144
def gray?
  rgb_color? && to_rgb_triple.gray?
end
rgb_color?() click to toggle source
# File lib/term/ansicolor/attribute.rb, line 136
def rgb_color?
  !!@rgb && !@true_color
end
to_rgb_triple() click to toggle source
# File lib/term/ansicolor/attribute.rb, line 148
def to_rgb_triple
  @rgb
end
true_color?() click to toggle source
# File lib/term/ansicolor/attribute.rb, line 140
def true_color?
  !!(@rgb && @true_color)
end